package com.fancy.mobileschool.ui.adapt;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.fancy.mobileschool.MyConfiguration;
import com.fancy.mobileschool.R;
import com.fancy.mobileschool.entity.GroupChatMessage;

import java.util.List;

/**
 * Created by Jackie on 2017/11/4.
 */

public class ChatGroupMessageAdapter extends RecyclerView.Adapter<ChatGroupMessageAdapter.ChatMessageItem> {

    private Context context;

    private List<GroupChatMessage> messages;

    private final static int RIGHT_MESSAGE = 1;

    private final static int LEFT_MESSAGE = 0;

    public ChatGroupMessageAdapter(Context context, List<GroupChatMessage> messages) {
        this.context = context;
        this.messages = messages;
    }

    @Override
    public ChatMessageItem onCreateViewHolder(ViewGroup parent, int viewType) {
        View view;
        if (viewType == RIGHT_MESSAGE)
            view = LayoutInflater.from(context).inflate(R.layout.chat_right_message_item, parent, false);
        else
            view = LayoutInflater.from(context).inflate(R.layout.chat_group_left_message_item, parent, false);

        return new ChatMessageItem(view);
    }

    @Override
    public void onBindViewHolder(ChatMessageItem holder, int position) {
        if (messages.get(position).person_from_id.equals(MyConfiguration.myID)) {
            holder.content.setText(messages.get(position).content);
        }
        else{
            holder.content.setText(messages.get(position).content);
            holder.name.setText(messages.get(position).name);
        }
    }

    @Override
    public int getItemCount() {
        return messages.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (messages.get(position).person_from_id.equals(MyConfiguration.myID))
            return RIGHT_MESSAGE;
        else
            return LEFT_MESSAGE;
    }

    public void putDate(List<GroupChatMessage> messages) {
        this.messages = messages;
    }

    public void addDate(GroupChatMessage message) {
        messages.add(message);
    }

    class ChatMessageItem extends RecyclerView.ViewHolder {

        private TextView name;

        private TextView content;

        public ChatMessageItem(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.person_name);
            content = (TextView) itemView.findViewById(R.id.chat_room_message_text);
        }
    }
}
